﻿Imports System.Runtime.InteropServices

Public Enum TypeDictionary
    Native = 100
    Foreign = 200
End Enum

Public Class frmViewerDictionary


    Public IsSecondTime As Boolean = False
    Public IsURL As Boolean = False
    Private IniTop As Long
    Private IniLeft As Long
    Private TypeDict As TypeDictionary


    <DllImport("User32.dll")> _
    Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    End Function

    <DllImport("User32.dll")> _
    Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
    End Function

    Private Sub EndFormExecution()


        If My.Settings.Configured Then

            UnProgramHK(TypeDictionary.Foreign)

        End If


        If My.Settings.NativeConfigured Then

            UnProgramHK(TypeDictionary.Native)

        End If

    End Sub




    Private Sub frmViewerDictionary_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If e.CloseReason = CloseReason.UserClosing Then

            Dim stateTopMost As Boolean = Me.TopMost

            Me.TopMost = False

            Me.Visible = False

            Me.TopMost = stateTopMost

            e.Cancel = True

        Else

            EndFormExecution()

        End If

    End Sub


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not My.Settings.Configured Then

            MsgBox("Please, first of all configure your settings in Babel")

            frmConf.ShowDialog()

            If Not My.Settings.Configured Then

                MsgBox("Sorry, if you dont configure this app dont run properly")
                Me.Close()

            End If

        End If

        Dim msgNative As String = ""
        Dim msgForeign As String = ""

        If My.Settings.Configured Then
            ProgramHK(TypeDictionary.Foreign)
            msgForeign = " <" & _
            IIf(My.Settings.Alt, "Alt+", "") & _
            IIf(My.Settings.Ctrl, "Ctrl+", "") & _
            IIf(My.Settings.Shift, "Shift+", "") & _
            CType(Val(My.Settings.HotKey), System.Windows.Forms.Keys).ToString & _
            "> "
        End If

        If My.Settings.NativeConfigured Then
            ProgramHK(TypeDictionary.Native)
            msgNative = " <" & _
            IIf(My.Settings.NativeAlt, "Alt+", "") & _
            IIf(My.Settings.NativeCtrl, "Ctrl+", "") & _
            IIf(My.Settings.NativeShift, "Shift+", "") & _
            CType(Val(My.Settings.NativeHotKey), System.Windows.Forms.Keys).ToString & _
            "> "
        End If

        Dim Msg As String = ""

        If My.Settings.Configured Or My.Settings.NativeConfigured Then

            Msg = "Press "
            If My.Settings.Configured Then Msg &= msgForeign
            If My.Settings.NativeConfigured Then Msg &= msgNative
            Msg &= " or " & vbCrLf

        Else
            Msg = "Click "
        End If

        Msg &= "<left mouse> on tray icon"

        If Msg.Length > 63 Then Msg = Mid(Msg, 1, 63)

        NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
        NotifyIcon1.BalloonTipText = Msg
        NotifyIcon1.BalloonTipTitle = "Babel traduction services"
        NotifyIcon1.Icon = My.Resources.BabelIconB
        NotifyIcon1.Visible = True
        NotifyIcon1.Text = Msg
        NotifyIcon1.ShowBalloonTip(50000)

        NotifyIcon1.ContextMenuStrip = New ContextMenuStrip()
        NotifyIcon1.ContextMenuStrip.Items.Add(New ToolStripMenuItem("&About", Me.Icon.ToBitmap, AddressOf About))
        NotifyIcon1.ContextMenuStrip.Items.Add(New ToolStripMenuItem("&Configuration", Me.Icon.ToBitmap, AddressOf MenuConfiguration))
        NotifyIcon1.ContextMenuStrip.Items.Add(New ToolStripMenuItem("&Show Last", Me.Icon.ToBitmap, AddressOf MenuShowMe))
        NotifyIcon1.ContextMenuStrip.Items.Add(New ToolStripMenuItem("&Exit", Me.Icon.ToBitmap, AddressOf MenuExit))

        Me.IniTop = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
        Me.IniLeft = Screen.PrimaryScreen.WorkingArea.Width - Me.Width

        Me.Top = Me.IniTop
        Me.Left = Me.IniLeft

    End Sub


    Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        ActivateForm()
    End Sub

    Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        If e.Button = MouseButtons.Left Then
            ActivateForm()
        End If

    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        ActivateForm()
    End Sub

    Private Sub ActivateForm()

        Dim PasteWordURL As Boolean
        Dim URL As String = ""

        ShowMe()

        Select Case TypeDict

            Case TypeDictionary.Foreign
                PasteWordURL = My.Settings.PasteWordURL
                URL = My.Settings.URL
                '"http://navclient.gmodules.com/gadgets/ifr?synd=navclient&oe=utf8&w=271&h=587&lang=es&country=ES&url=http%3a%2f%2fwww.labpixies.com%2fcampaigns%2fbabylon%2fbabylon.xml&parent=http%3a%2f%2ftools.google.com%2ftoolbar%2fbuttons%2fgadgetfiles&up_babylonlang=ES"

            Case TypeDictionary.Native
                PasteWordURL = My.Settings.NativePasteWordURL
                URL = My.Settings.NativeURL

        End Select

        If PasteWordURL Then

            If Clipboard.ContainsText Then

                URL &= "/" & Clipboard.GetText

            End If

        End If

        Me.WebBrowser1.Url = New System.Uri(URL)
        Me.IsURL = True
        Me.WebBrowser1.Navigate(New System.Uri(URL))

    End Sub


    Private Function GetConfForHotHey(ByVal TD As TypeDictionary) As Babel.My.MySettings

        Dim conf As Babel.My.MySettings = New Babel.My.MySettings

        Select Case TD

            Case TypeDictionary.Foreign

                conf.Configured = My.Settings.Configured
                conf.Paste = My.Settings.Paste
                conf.ID = My.Settings.ID
                conf.EnableSendKeys = My.Settings.EnableSendKeys
                conf.KeysToSend = My.Settings.KeysToSend
                conf.PasteWordURL = My.Settings.PasteWordURL
                conf.Alt = My.Settings.Alt
                conf.HotKey = My.Settings.HotKey
                conf.Ctrl = My.Settings.Ctrl
                conf.Shift = My.Settings.Shift
                conf.Win = My.Settings.Win


            Case TypeDictionary.Native

                conf.Configured = My.Settings.NativeConfigured
                conf.Paste = My.Settings.NativePaste
                conf.ID = My.Settings.NativeID
                conf.EnableSendKeys = My.Settings.NativeEnableSendKeys
                conf.KeysToSend = My.Settings.NativeKeysToSend
                conf.PasteWordURL = My.Settings.NativePasteWordURL
                conf.Alt = My.Settings.NativeAlt
                conf.HotKey = My.Settings.NativeHotKey
                conf.Ctrl = My.Settings.NativeCtrl
                conf.Shift = My.Settings.NativeShift
                conf.Win = My.Settings.NativeWin

        End Select

        Return conf

    End Function

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        Dim conf As Babel.My.MySettings = GetConfForHotHey(TypeDict)

        If Me.IsURL Then

            Me.IsURL = False

            If Clipboard.ContainsText And conf.Configured And conf.Paste Then

                Me.WebBrowser1.Document.GetElementById(conf.ID).InnerText = Clipboard.GetText

                AppActivate(My.Application.Info.Title)

                Me.Focus()
                Me.WebBrowser1.Focus()


                If My.Settings.EnableSendKeys Then

                    My.Computer.Keyboard.SendKeys(conf.KeysToSend, False)

                End If

            End If



        End If

    End Sub

    Private Sub MenuExit(ByVal sender As Object, ByVal e As System.EventArgs)

        EndFormExecution()

        My.Application.ApplicationContext.ExitThread()

    End Sub


    Private Sub About(ByVal sender As Object, ByVal e As System.EventArgs)
        frmAbout.ShowDialog()
    End Sub

    Private Sub MenuConfiguration(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim menu As System.Windows.Forms.ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)

        frmConf.ShowDialog()

        If My.Settings.Configured Then

            UnProgramHK(TypeDictionary.Foreign)
            ProgramHK(TypeDictionary.Foreign)

        End If


        If My.Settings.NativeConfigured Then

            UnProgramHK(TypeDictionary.Native)
            ProgramHK(TypeDictionary.Native)

        End If

        'Me.WebBrowser1.Url = New System.Uri(My.Settings.URL)

    End Sub

    Private Sub ProgramHK(ByVal TypeHK As TypeDictionary)

        Dim conf As Babel.My.MySettings = GetConfForHotHey(TypeHK)

        Dim HKD As New HotKeyDefs(conf.Alt, conf.Shift, conf.Ctrl, conf.Win)
        RegisterHotKey(Me.Handle, TypeHK, HKD.ModifierFlags, CType(Val(conf.HotKey), System.Windows.Forms.Keys))

    End Sub

    Private Sub UnProgramHK(ByVal TypeHK As TypeDictionary)

        Try

            UnregisterHotKey(Me.Handle, TypeHK)

        Catch ex As Exception

        End Try

    End Sub


    Private Sub MenuShowMe(ByVal sender As Object, ByVal e As System.EventArgs)

        ShowMe()

    End Sub

    Private Sub ShowMe()

        Me.Visible = True
        Me.TopMost = True
        Me.Top = Me.IniTop
        Me.Left = Me.IniLeft
        Me.Focus()

    End Sub

    Private Sub frmViewerDictionary_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd

        IniTop = Me.Top
        IniLeft = Me.Left

    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = HotKeyDefs.WM_HOTKEY Then

            Dim id As IntPtr = m.WParam

            Select Case (CType(id.ToInt32, TypeDictionary))

                Case TypeDictionary.Foreign

                    TypeDict = TypeDictionary.Foreign
                    ActivateForm()

                Case TypeDictionary.Native

                    TypeDict = TypeDictionary.Native
                    ActivateForm()

            End Select

        End If

        MyBase.WndProc(m)

    End Sub

    Private Sub frmViewerDictionary_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        If Not Me.IsSecondTime Then
            Me.IsSecondTime = True
            Me.Visible = False
        End If

    End Sub
End Class
